package com.zjy.pet.service.impl;

import com.zjy.org.domain.Department;
import com.zjy.pet.domain.PetType;
import com.zjy.pet.mapper.PetTypeMapper;
import com.zjy.pet.service.PetTypeService;
import com.zjy.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements PetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> petTypeTree() {
        List<PetType> petTypes = petTypeMapper.findAll();
        HashMap<Long, PetType> map = new HashMap<>();
        for (PetType petType:petTypes){
            map.put(petType.getId(),petType);
        }
        ArrayList<PetType> petTypeTree = new ArrayList<>();
        for(PetType petType:petTypes){
            if (petType.getParentId()==null){//判断为顶级部门
                petTypeTree.add(petType);
            }else {//否则就为次级部门
                Long parent_id = petType.getParentId();
                PetType parentPetType = map.get(parent_id);
                if(parentPetType!=null){
                    parentPetType.getChildren().add(petType);
                }
            }
        }
        return petTypeTree;
    }
}
